<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class EmailList extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('cmladministration','refresh');
	    }
        if($_SESSION['role_id'] != 1)
        {
            redirect('cmladmin/accessdenied','refresh');
        }
        $this->load->model('MSubscriber');
        $this->tinyMce = '
		<!-- TinyMCE -->
		<script type="text/javascript" src="'. base_url().'media/admin/js/tiny_mce/tiny_mce.js"></script>
		<script type="text/javascript">
	 		tinyMCE.init({
        		// General options
        		mode : "textareas",
        		theme : "advanced",
        		plugins : "openmanager,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks",
        
        		// Theme options
        		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,openmanager",
        		theme_advanced_toolbar_location : "top",
        		theme_advanced_toolbar_align : "left",
        		theme_advanced_statusbar_location : "bottom",
        		theme_advanced_resizing : true,
        		
        		//Open Manager Options
        		file_browser_callback: "openmanager",
        		open_manager_upload_path: "../../../uploads/",
        
        		// Example content CSS (should be your site CSS)
        		content_css : "css/content.css",
        
        		// Drop lists for link/image/media/template dialogs
        		template_external_list_url : "lists/template_list.js",
        		external_link_list_url : "lists/link_list.js",
        		external_image_list_url : "lists/image_list.js",
        		media_external_list_url : "lists/media_list.js",
        
        		// Style formats
        		style_formats : [
        			{title : "Bold text", inline : "b"},
        			{title : "Red text", inline : "span", styles : {color : "#ff0000"}},
        			{title : "Red header", block : "h1", styles : {color : "#ff0000"}},
        			{title : "Example 1", inline : "span", classes : "example1"},
        			{title : "Example 2", inline : "span", classes : "example2"},
                    {title : "History Paragraph", inline : "p", classes : "teswtafdd"},
        			{title : "Table styles"},
        			{title : "Table row 1", selector : "tr", classes : "tablerow1"}
        		],
        
        		// Replace values for the template plugin
        		template_replace_values : {
        			username : "Some User",
        			staffid : "991234"
        		}
        	});
		</script>
		<!-- /TinyMCE -->
		';
    }
    
    public function index()
    {
        $data['title'] = 'Subscriber List';
		$data['main'] = 'admin/emaillist';
        $data['emailList'] = $this->MSubscriber->getSubscriberList(); 
        $data['templateList'] = $this->MSubscriber->getTemplateList();
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function send()
    {
        if($this->input->post('emails'))
        {
    		$config = Array(
    			  'protocol' => 'smtp',
    			  'smtp_host' => 'mail.chamroeun.com',
    			  'smtp_port' => 25,//465,
    			  'smtp_user' => 'bunthorn@chamroeun.com', // change it to yours
    			  'smtp_pass' => 'sZad9tHg', // change it to yours
    			  'mailtype' => 'html',
    			  'charset' => 'utf-8'
    		);	
            $list = $this->input->post('emails');
    		$this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->set_mailtype("html");
    		$this->email->from('bunthorn@chamroeun.com', "Chamroeun Microfinance Limited.");
    		$this->email->to($list);
            $template = $this->MSubscriber->getTemplateById($this->input->post('template_id'));
            
    		$this->email->subject($template['subject']);
    		$this->email->message($template['body']);
    			
    		$message = "Sorry unable to send email...";	
    		if($this->email->send()){					
    			$message = "The email has been sent to those correct emails.";			
    		}	
        	$this->session->set_flashdata('message',$message);
        }
  		redirect('emaillist','refresh');
    }
    
    public function addTemplate()
    {
        if($this->input->post('subject'))
        {
            $this->MSubscriber->addEditTemplate();
            $this->session->set_flashdata('message','Template created!');
  		    redirect('emaillist','refresh');
        }
        $data['title'] = "Add Email Form";
		$data['main'] = 'admin/templateadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('emaillist');
		$this->load->vars($data);
		$this->load->view('admin/layout'); 
    }
    
    public function editTemplate($id=0)
    {
        if($this->input->post('subject'))
        {
            $this->MSubscriber->addEditTemplate($this->input->post('template_id'));
            $this->session->set_flashdata('message','Template edited!');
  		    redirect('emaillist','refresh');
        }
        $data['title'] = "Add Email Form";
		$data['main'] = 'admin/templateedit';
        $data['template'] = $this->MSubscriber->getTemplateById($id);
        $data['form'] = $this->MFormGlobal->getButtonsAdd('emaillist');
		$this->load->vars($data);
		$this->load->view('admin/layout'); 
    }
    
}